﻿using Coldairarrow.Entity.MEGA;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.MEGA
{
    public class MEGA_MaterialBusiness : BaseBusiness<MEGA_Material>, IMEGA_MaterialBusiness, ITransientDependency
    {
        public MEGA_MaterialBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口

        public async Task<PageResult<MEGA_Material>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<MEGA_Material>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<MEGA_Material, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            var qq = from a in q
                     join b in Db.GetIQueryable<MEGA_MaterialCategory>() on a.TypeId equals b.Id
                     select new MEGA_Material
                     {
                         Batch = a.Batch,
                         Code = a.Code,
                         CreateTime = a.CreateTime,
                         CreatorId = a.CreatorId,
                         Deleted = a.Deleted,
                         Editor = a.Editor,
                         EditTime = a.EditTime,
                         Id = a.Id,
                         Image = a.Image,
                         JGTime = a.JGTime,
                         Name = a.Name,
                         Note = a.Note,
                         Num = a.Num,
                         QrCodeUrl = a.QrCodeUrl,
                         State = a.State,
                         TypeId = a.TypeId,
                         TypeName = b.Name
                     };
            return await qq.Where(where).GetPageResultAsync(input);
        }

        public async Task<MEGA_Material> GetTheDataAsync(string id)
        {
            var q = GetIQueryable().Where(x => x.Id.Equals(id));
            var qq = await (from a in q
                            join b in Db.GetIQueryable<MEGA_MaterialCategory>() on a.TypeId equals b.Id
                            select new MEGA_Material
                            {
                                Batch = a.Batch,
                                Code = a.Code,
                                CreateTime = a.CreateTime,
                                CreatorId = a.CreatorId,
                                Deleted = a.Deleted,
                                Editor = a.Editor,
                                EditTime = a.EditTime,
                                Id = a.Id,
                                Image = a.Image,
                                JGTime = a.JGTime,
                                Name = a.Name,
                                Note = a.Note,
                                Num = a.Num,
                                QrCodeUrl = a.QrCodeUrl,
                                State = a.State,
                                TypeId = a.TypeId,
                                TypeName = b.Name
                            }).FirstOrDefaultAsync();
            return qq;
        }

        public async Task AddDataAsync(MEGA_Material data)
        {
            //string code = DateTime.Now.ToJsTimestamp().ToString().Substring(5, 8);
            ////生成随机8位数字
            //data.Code = "YQ" + code;
            var q = Db.GetIQueryable<MEGA_Material>().Where(x => x.Name.Equals(data.Name)).ToList();
            if (q.Count() > 0)
            {
                throw new Exception("该叶片已经存在!");
            }
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(MEGA_Material data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        /// <summary>
        /// 根据材料id查询出相关叶片信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<List<MEGA_LeafBlade>> GetLeafBladeData(string id)
        {
            var q = Db.GetIQueryable<MEGA_LeafBlade>().Where(x => x.MaterialId.Equals(id)).ToListAsync();

            return await q;
        }

        #endregion

        #region 私有成员

        #endregion
    }
}